package com.guiwu.gteway.industryapp.ChangePassword;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.guiwu.gteway.industryapp.Data.DataRepository;
import com.guiwu.gteway.industryapp.Data.MDataSource;
import com.guiwu.gteway.industryapp.Device.module.User;
import com.guiwu.gteway.industryapp.R;
import com.guiwu.gteway.industryapp.RootActivity;
import com.guiwu.gteway.industryapp.util.MyLog;
import com.guiwu.gteway.industryapp.util.PreferencesUtil;
import com.guiwu.gteway.industryapp.util.ProgressSimpleDialog;
import com.guiwu.gteway.industryapp.util.ToastUtil;
import com.guiwu.gteway.industryapp.widget.SimpleToolbar;


import static com.google.common.base.Preconditions.checkNotNull;

public class ChangeActivity extends RootActivity implements ChangePasswordContract.View {

    private static final String TAG = "ChangeActivity";
    public static final String ARG_PHONE_NUM = "com.guiwu.gteway.industryapp.ChangePassword.phoneNum";
    private TextView mPhoneNumTv;
    private EditText mInputCode;
    private ChangePasswordContract.Presenter mChangePresenter;
    private Button mAskVerifyCodeBt;
    private Button mChangeBt;
    private String returnVerifyCode;
    private ProgressSimpleDialog dialog;
    private EditText mOldPas;
    private EditText mNewPas;
    private User mUser;
    private SimpleToolbar simpleToolbar;

    private CountDownTimer timer = new CountDownTimer(60000,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            if(!isFinishing()){
                //doing something...
                mAskVerifyCodeBt.setText((millisUntilFinished / 1000) + "秒后可重发");
                mAskVerifyCodeBt.setEnabled(false);
            }

        }

        @Override
        public void onFinish() {
            if (!isFinishing()){
                mAskVerifyCodeBt.setText("获取验证码");
                mAskVerifyCodeBt.setEnabled(true);

            }
        }
    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_key_layout);
        //mPhoneNum = getIntent().getStringExtra(ARG_PHONE_NUM);
        initView();
        initData();
    }

    private void initData(){
        mChangePresenter = new ChangePresenter(this);
        dialog = new ProgressSimpleDialog(this);
        initUser();
        MyLog.i(TAG,"new Presenter");
    }

    private void initUser(){
        dialog.showPorgressDialog("正在获取用户信息");
        int userID = PreferencesUtil.getInstance(this).getInt("userId",0);
        DataRepository.getInstance().getUserInfo(String.valueOf(userID), new MDataSource.GetUserInfoCallback() {
            @Override
            public void onUserInfo(User user) {
                MyLog.i(TAG,user.toString());
                mUser = user;
                String currentNumNotice = getResources().getString(R.string.current_phone_tag);
                //  String currentPhoneNum = PreferencesUtil.getInstance(this).getString("phoneNum","error");
                mPhoneNumTv.setText(currentNumNotice + mUser.getPhoneNum());
                dialog.closeProgressDialog();
            }

            @Override
            public void onDataNotAvailable(String msg) {
                MyLog.i(TAG,msg);
                dialog.closeProgressDialog();
            }
        });
    }


    private void initView(){
        mPhoneNumTv = findViewById(R.id.current_phone_num);
        mInputCode = findViewById(R.id.change_et_register_verify_code);
        simpleToolbar = findViewById(R.id.change_toolbar);

        mAskVerifyCodeBt = findViewById(R.id.change_bt_register_send_verify);
        mAskVerifyCodeBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
                mChangePresenter.getVerificationCode(mUser.getPhoneNum());
            }
        });

        mChangeBt = findViewById(R.id.change_pw_confirm);
        mChangeBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkPas()){
                    mChangePresenter.askChangePsw(mNewPas.getText().toString(),mUser.getPhoneNum());
                }
            }
        });

        mNewPas = findViewById(R.id.new_password);
        mOldPas = findViewById(R.id.old_password);

        simpleToolbar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private boolean checkPas(){

        if (returnVerifyCode!=null && !returnVerifyCode.equals(mInputCode.getText().toString())){
            ToastUtil.showToast(this,"请输入正确的验证码");
            return false;
        }

//        if (!mUser.getPassword().equals(mOldPas.getText().toString())){
//            ToastUtil.showToast(this,"请输入正确的密码");
//            return false;
//        }

        if (!mOldPas.getText().toString().equals(mNewPas.getText().toString())){
            ToastUtil.showToast(this,"两次输入的密码不同");
            return false;
        }

        return true;
    }



    public static Intent newIntent(Context context){
        Intent intent = new Intent(context,ChangeActivity.class);
       // intent.putExtra(ARG_PHONE_NUM,phoneNum);
        return intent;
    }

    @Override
    public void showLoadingIndicator(boolean activity) {
        if (activity){
            dialog.showPorgressDialog("正在修改");
        }else {
            dialog.closeProgressDialog();
        }
    }

    @Override
    public void onChangePassword(boolean result, String msg) {
        ToastUtil.showToast(this,msg);
    }

    @Override
    public void onGetVerifyCode(String verifyCode) {
        MyLog.i(TAG,verifyCode);
        returnVerifyCode = verifyCode;
    }

    @Override
    public void showGetVerifyCodeError(String errorMsg) {
        ToastUtil.showToast(this,errorMsg);
        MyLog.i(TAG,errorMsg);
    }

    @Override
    public void setPresenter(ChangePasswordContract.Presenter presenter) {
        mChangePresenter = checkNotNull(presenter);
        MyLog.i(TAG,"set Presenter");
    }

    @Override
    protected void onDestroy() {
        if(timer!=null){
            timer.cancel();
            timer = null;
        }
        super.onDestroy();
    }

}
